package transclusion;

import processing.core.*;
import transclusion.items.*;
import treemap.*;

public class SourceItem extends SimpleMapItem {
	DAG dag;
	PApplet papplet;
	CategoryItem parent;
	Itemable item;
	String name;
	int level;

	int color;
	float hue;
	float brightness;

	float textPadding = 8;

	float boxLeft, boxTop;
	float boxRight, boxBottom;	

	SourceItem(DAG d, CategoryItem parent, Itemable itm, int level, int order) {
		this.dag = d;
		this.papplet = dag.papplet;
		this.parent = parent;
		this.item = itm;
		this.order = order;
		this.level = level;

		name = item.name();
		size = item.length();

		((UniverseVisualization)papplet).modTimes.add(item.priority());
	}

	void updateColors() {
		if (parent != null) {
			//hue = PApplet.map(order, 0, parent.getItemCount(), 0, 360);			
			if( item.isCategory() )
				hue = PApplet.map(order, 0, parent.getItemCount(), 40, 320);
			else
				hue = 0;
		}
		
		brightness = ((UniverseVisualization)papplet).modTimes.percentile(item.priority()) * 100;
		
		/**
		if( item.isCategory() )		
			brightness = ((UniverseVisualization)papplet).modTimes.percentile(item.priority()) * 100;
		else 	
			brightness = 90;
		*/		

		papplet.colorMode(PApplet.HSB, 360, 100, 100);
		if (parent == ((UniverseVisualization)papplet).zoomItem) {
			color = papplet.color(hue, 80, 80);
		} else if (parent != null) {			
			//color = papplet.color(parent.hue, 80, brightness);
			///*
			if( item.isCategory() )
				color = papplet.color(parent.hue, 80, brightness);
			else
				color = papplet.color(0, 80, brightness);
				//color = papplet.color(0, 80, 90);
			//*/
		}
		papplet.colorMode(PApplet.RGB, 255);
	}

	void calcBox() {
		boxLeft = ((UniverseVisualization)papplet).zoomBounds.spanX(x, 0, papplet.width);
		boxRight = ((UniverseVisualization)papplet).zoomBounds.spanX(x + w, 0, papplet.width);
		boxTop = ((UniverseVisualization)papplet).zoomBounds.spanY(y, 0, papplet.height);
		boxBottom = ((UniverseVisualization)papplet).zoomBounds.spanY(y + h, 0, papplet.height);
	}

	public void draw() {
		calcBox();

		papplet.fill(color);
		papplet.rect(boxLeft, boxTop, boxRight, boxBottom);

		if (textFits()) {
			drawTitle();
		} else if (mouseInside()) {
			((UniverseVisualization)papplet).rolloverItem = this;
		}
	}

	void drawTitle() {
		papplet.fill(255, 200);

		float middleX = (boxLeft + boxRight) / 2;
		float middleY = (boxTop + boxBottom) / 2;
		if (middleX > 0 && middleX < papplet.width && middleY > 0
				&& middleY < papplet.height) {
			if (boxLeft + papplet.textWidth(name) + textPadding * 2 > papplet.width) {
				papplet.textAlign(PApplet.RIGHT);
				papplet.text(name,papplet.width - textPadding, boxBottom - textPadding);
			} else {
				papplet.textAlign(PApplet.LEFT);
				papplet.text(name, boxLeft + textPadding, boxBottom - textPadding);
			}
		}
	}

	boolean textFits() {
		float wide = papplet.textWidth(name) + textPadding * 2;
		float high = papplet.textAscent() + papplet.textDescent() + textPadding * 2;
		return (boxRight - boxLeft > wide) && (boxBottom - boxTop > high);
	}

	boolean mouseInside() {
		return (papplet.mouseX > boxLeft && papplet.mouseX < boxRight && papplet.mouseY > boxTop && papplet.mouseY < boxBottom);
	}

	boolean mouseClicked() {
		if (mouseInside()) {
			if (papplet.mouseButton == PApplet.LEFT)
				open();
			else if (papplet.mouseButton == PApplet.RIGHT)
				close();
			return true;
		}
		return false;
	}
	
	void open() {
		parent.zoomIn();
		if( parent.reinvokedZoom ) {
			((UniverseVisualization) papplet).sourceArticleItem = this;
			((UniverseVisualization) papplet).transclusionIsShown = true;
			((UniverseVisualization) papplet).cursor(PApplet.ARROW);
		}
	}	
	
	void close() {
		if (parent == ((UniverseVisualization)papplet).zoomItem) {
			parent.zoomOut();
		} else {
			parent.hideContents();
		}
	}
}